﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_AdminUser : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            base.loadGroups(ddlGroup, 0);
            getAdministrators();
            if (Request["g_id"] != null)
                ddlGroup.SelectedValue = Request["g_id"].ToString();
        }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(4, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "txtTitle":
                    txtTitle.InnerText = value;
                    break;
                case "btnNew":
                    btnNew.Text = value;
                    break;
                case "Page_size":
                    int ps = 0;
                    Int32.TryParse(value, out ps);
                    if (ps > 0)
                        base.Page_size = ps;
                    break;
            }
        }
    }
    protected void getAdministrators()
    {
        try
        {
            objDT = BO_Sec_Administrators.SelectGroup(1, Convert.ToInt32(ddlGroup.SelectedValue), base.Cur_page, base.Page_size);
            bindRepeater(rptData, objDT, divMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["g_id"] != null)
        {
            int conti = url.LastIndexOf("g_id=");
            string start = url.Substring(0, url.LastIndexOf("&g_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&g_id=" + ddlGroup.SelectedValue);
    }
    protected void btnNew_Click(object sender, EventArgs e) { Response.Redirect("default.aspx?module=adminuseradd"); }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.Equals("del"))
        {
            BO_Sec_Administrators.Delete(Convert.ToInt32(e.CommandArgument));
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            getAdministrators();
        }
    }
}
